package com.power.using.mybomb.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.power.using.mybomb.R;
import com.power.using.mybomb.base.ImageLoaderFactory;
import com.power.using.mybomb.base.ParentWithNaviActivity;
import com.power.using.mybomb.bean.AddFriendMessage;
import com.power.using.mybomb.bean.User;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.newim.BmobIM;
import cn.bmob.newim.bean.BmobIMConversation;
import cn.bmob.newim.bean.BmobIMMessage;
import cn.bmob.newim.bean.BmobIMUserInfo;
import cn.bmob.newim.core.BmobIMClient;
import cn.bmob.newim.core.ConnectionStatus;
import cn.bmob.newim.listener.MessageSendListener;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;

/**
 * Created by 聪明一只哈 on 2020/3/21.
 * Time: 2020/3/21  10:36
 */
public class UserInfoActivity extends ParentWithNaviActivity {
    @BindView(R.id.v_top)
    View vTop;
    @BindView(R.id.tv_left)
    ImageView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.iv_avator)
    ImageView ivAvator;
    @BindView(R.id.layout_head)
    RelativeLayout layoutHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.layout_name)
    RelativeLayout layoutName;
    @BindView(R.id.btn_add_friend)
    Button btnAddFriend;
    @BindView(R.id.btn_chat)
    Button btnChat;
    @BindView(R.id.layout_all)
    LinearLayout layoutAll;
    private User user;
    private BmobIMUserInfo info;

    @Override
    protected String title() {
        return null;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        ButterKnife.bind(this);

        initNaviView();
        user = (User) getBundle().getSerializable("u");
        //用户为登录用户
        if(user.getObjectId().equals(getCurrentUid())){
            btnAddFriend.setVisibility(View.GONE);
            btnChat.setVisibility(View.GONE);
        }else{//用户为非登录用户
            btnAddFriend.setVisibility(View.VISIBLE);
            btnChat.setVisibility(View.VISIBLE);
        }

        //构造聊天方的用户信息：传入id、用户和用户头像三个参数
        info = new BmobIMUserInfo(user.getObjectId(), user.getUsername(), user.getAvatar());
        //加载头像
        ImageLoaderFactory.getLoader().loadAvator(ivAvator,user.getAvatar(),R.mipmap.head);
        //显示名称
        tvName.setText(user.getUsername());
    }

    @OnClick({R.id.btn_add_friend, R.id.btn_chat})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_add_friend:
                sendAddFriendMessage();
                break;
            case R.id.btn_chat:
                chat();
                break;
        }
    }



    /**
     * 发送添加好友请求
     */
    private void sendAddFriendMessage() {

        if(BmobIM.getInstance().getCurrentStatus().getCode()!= ConnectionStatus.CONNECTED.getCode()){
            toast("尚未连接IM服务器");
            return;
        }

        //TODO 会话：4.1、创建一个暂态会话入口，发送好友请求
        BmobIMConversation conversationEntrance = BmobIM.getInstance().startPrivateConversation(info, true, null);
        //TODO 消息：5.1、根据会话入口获取消息管理，发送好友请求
        BmobIMConversation messageManager = BmobIMConversation.obtain(BmobIMClient.getInstance(), conversationEntrance);
        AddFriendMessage msg = new AddFriendMessage();
        User currentUser = BmobUser.getCurrentUser(User.class);
        msg.setContent("可以加个好友吗？");
        Map<String,Object> map=new HashMap<>();
        map.put("name",currentUser.getUsername());
        map.put("avatar",currentUser.getAvatar());
        map.put("uid",currentUser.getObjectId());

        msg.setExtraMap(map);
        messageManager.sendMessage(msg, new MessageSendListener() {
            @Override
            public void done(BmobIMMessage bmobIMMessage, BmobException e) {
                 if(e==null){
                     toast("好友请求发送成功，等待验证");
                 }else{
                     toast("发送失败："+e.getMessage());
                 }
            }
        });
    }

    /**
     * 与陌生人聊天
     */
    private void chat() {
        if(BmobIM.getInstance().getCurrentStatus().getCode()!=ConnectionStatus.CONNECTED.getCode()){
            toast("尚未链接IM服务器");
            return;
        }

        //TODO 会话：4.1、创建一个常态会话入口，陌生人聊天
        BmobIMConversation conversationEntrance = BmobIM.getInstance().startPrivateConversation(info, null);
        Bundle bundle = new Bundle();
        bundle.putSerializable("c",conversationEntrance);
        startActivity(ChatActivity.class,bundle,false);

    }
}
